package com.tao.lab.bitmaprecycle;

import com.tao.lab.utils.Util;

import java.util.Queue;

/**
 * @ProjectName: lab
 * @Package: com.tao.lab.bitmaprecycle
 * @ClassName: BaseKeyPool
 * @Description: java类作用描述
 * @Author: wangxf
 * @CreateDate: 2022/5/26 9:22
 * @UpdateUser: 更新者
 * @UpdateDate: 2022/5/26 9:22
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
abstract class BaseKeyPool<T extends Poolable> {
    private static final int MAX_SIZE = 20;
    private final Queue<T> keyPool = Util.createQueue(20);

    BaseKeyPool() {
    }

    protected T get() {
        T result = (T) this.keyPool.poll();
        if (result == null) {
            result = this.create();
        }

        return result;
    }

    public void offer(T key) {
        if (this.keyPool.size() < 20) {
            this.keyPool.offer(key);
        }

    }

    protected abstract T create();
}

